package com.txy.test.domain.strategy.service.rule.tree;

import com.alibaba.fastjson.JSON;
import com.txy.domain.strategy.model.vo.*;
import com.txy.domain.strategy.service.rule.tree.factory.DefaultTreeFactory;
import com.txy.domain.strategy.service.rule.tree.factory.engine.IDecisionTreeEngine;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class LogicTreeTest {
    @Resource
    private DefaultTreeFactory defaultTreeFactory;

    @Test
    public void testLogicTree() {
        RuleTreeNodeVO ruleLockNode = RuleTreeNodeVO.builder()
                .treeId("10000")
                .ruleKey("rule_lock")
                .ruleDesc("奖品指定次数解锁")
                .ruleValue("3")
                .treeNodeLineVOList(new ArrayList<RuleTreeNodeLineVO>() {{
                    add(RuleTreeNodeLineVO.builder()
                            .treeId("10000")
                            .ruleNodeFrom("rule_lock")
                            .ruleNodeTo("rule_luck_award")
                            .ruleLimitType(RuleLimitTypeVO.EQUAL)
                            .ruleLimitValue(RuleLogicCheckTypeVO.TAKE_OVER).build());
                    add(RuleTreeNodeLineVO.builder()
                            .treeId("10000")
                            .ruleNodeFrom("rule_lock")
                            .ruleNodeTo("rule_stock")
                            .ruleLimitType(RuleLimitTypeVO.EQUAL)
                            .ruleLimitValue(RuleLogicCheckTypeVO.ALLOW).build());
                }}).build();

        RuleTreeNodeVO ruleLuckAwardNode = RuleTreeNodeVO.builder()
                .treeId("10000")
                .ruleKey("rule_luck_award")
                .ruleDesc("幸运奖品")
                .ruleValue("3")
                .treeNodeLineVOList(new ArrayList<>()).build();

        RuleTreeNodeVO ruleStockNode = RuleTreeNodeVO.builder()
                .treeId("10000")
                .ruleKey("rule_stock")
                .ruleDesc("库存")
                .ruleValue("3")
                .treeNodeLineVOList(new ArrayList<RuleTreeNodeLineVO>() {{
                    add(RuleTreeNodeLineVO.builder()
                            .treeId("10000")
                            .ruleNodeFrom("rule_stock")
                            .ruleNodeTo("rule_luck_award")
                            .ruleLimitType(RuleLimitTypeVO.EQUAL)
                            .ruleLimitValue(RuleLogicCheckTypeVO.TAKE_OVER).build());
                }}).build();

        RuleTreeVO ruleTreeVO = RuleTreeVO.builder()
                .treeId("10000")
                .treeName("test")
                .treeDesc("规则树-test")
                .treeRootRuleNode("rule_lock")
                .treeNodeMap(new HashMap<String, RuleTreeNodeVO>() {{
                    put("rule_lock", ruleLockNode);
                    put("rule_luck_award", ruleLuckAwardNode);
                    put("rule_stock", ruleStockNode);
                }}).build();

        IDecisionTreeEngine iDecisionTreeEngine = defaultTreeFactory.openLoginRuleTree(ruleTreeVO);

        DefaultTreeFactory.AwardResClass process = iDecisionTreeEngine.process(100001L, "txy", 100);
        log.info(JSON.toJSONString(process));
    }
}
